package de.devisnik.eidle;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.preferences.ScopedPreferenceStore;

import de.devisnik.eidle.screen.IScreen;

public class EidlePreferencePage extends PreferencePage implements
		IWorkbenchPreferencePage {

	private Table screenList;
	private Button quitOnMouseMoved;

	public EidlePreferencePage() {
		super("eidle-prefs");
		setPreferenceStore(new ScopedPreferenceStore(new InstanceScope(),
				EidleActivator.PLUGIN_ID));
	}

	protected Control createContents(final Composite parent) {
		final Composite composite = createComposite(parent);
		createGeneralSettings(composite);
		createScreenSelection(composite);
		return composite;
	}

	private void createGeneralSettings(final Composite composite) {
		quitOnMouseMoved = new Button(composite, SWT.CHECK);
		quitOnMouseMoved.setText("quit on mouse moved");
		quitOnMouseMoved.setSelection(getPreferenceStore().getBoolean(
				EidlePreferences.QUIT_ON_MOUSE_DOWN));
	}

	protected Composite createComposite(final Composite parent) {
		final Composite composite = new Composite(parent, SWT.NULL);
		final GridLayout layout = new GridLayout();
		layout.marginWidth = 0;
		layout.marginHeight = 0;
		composite.setLayout(layout);
		final GridData data = new GridData(GridData.FILL_BOTH
				| GridData.VERTICAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_FILL);
		composite.setLayoutData(data);
		composite.setFont(parent.getFont());

		return composite;
	}

	protected void createScreenSelection(final Composite parent) {
		final Label label = new Label(parent, SWT.NONE);
		label.setText("select screens to be enabled:");
		label.setFont(parent.getFont());
		GridData data = new GridData(GridData.FILL_HORIZONTAL);
		label.setLayoutData(data);
		screenList = new Table(parent, SWT.BORDER | SWT.CHECK | SWT.H_SCROLL
				| SWT.V_SCROLL);
		data = new GridData(GridData.FILL_BOTH);
		screenList.setFont(parent.getFont());
		screenList.setLayoutData(data);
		screenList.addSelectionListener(new SelectionAdapter() {

			public void widgetSelected(final SelectionEvent e) {
				if (isValidateScreenList()) {
					setErrorMessage(null);
					setValid(true);
				} else {
					setErrorMessage("at least one screen must be selected!");
					setValid(false);
				}
			}
		});

		populateScreenList();
	}

	protected boolean isValidateScreenList() {
		final TableItem[] items = screenList.getItems();
		for (int i = 0; i < items.length; i++) {
			if (items[i].getChecked()) {
				return true;
			}
		}
		return false;
	}

	private void populateScreenList() {
		final IScreen[] screens = EidleActivator.getDefault().getScreens();

		// sort screens by name
		Collections.sort(Arrays.asList(screens), new Comparator() {

			public int compare(final Object o1, final Object o2) {
				final IScreen left = (IScreen) o1;
				final IScreen right = (IScreen) o2;
				return left.getName().compareTo(right.getName());
			}
		});
		for (int i = 0; i < screens.length; i++) {
			final IScreen screen = screens[i];
			final TableItem item = new TableItem(screenList, SWT.NONE);
			item.setText(screen.getName());
			item.setChecked(getPreferenceStore().getBoolean(
					screen.getClass().getName()));
			item.setData(screen);
		}
	}

	public void init(final IWorkbench workbench) {
	}

	protected void performDefaults() {
		quitOnMouseMoved.setSelection(getPreferenceStore().getDefaultBoolean(
				EidlePreferences.QUIT_ON_MOUSE_DOWN));

		final TableItem items[] = screenList.getItems();
		for (int i = 0; i < items.length; i++) {
			final TableItem item = items[i];
			final IScreen screen = (IScreen) item.getData();
			item.setChecked(getPreferenceStore().getDefaultBoolean(
					screen.getClass().getName()));
		}
	}

	public boolean performOk() {
		getPreferenceStore().setValue(EidlePreferences.QUIT_ON_MOUSE_DOWN,
				quitOnMouseMoved.getSelection());

		final TableItem items[] = screenList.getItems();
		for (int i = 0; i < items.length; i++) {
			final TableItem item = items[i];
			final IScreen screen = (IScreen) item.getData();
			getPreferenceStore().setValue(screen.getClass().getName(),
					item.getChecked());
		}
		return true;
	}
}
